<template>
	<!-- 提货中心订单列表 -->
	<view style="overflow: hidden;background-color: #F3F3F3;">
		<!-- 顶部导航 -->
		<view class="topTabBar" :style="{ position: headerPosition, top: headerTop }">
			<view class="grid" v-for="(grid, tbIndex) in orderType" :key="tbIndex" @tap="showType(tbIndex)">
				<view class="text" :style="{color: tbIndex == tabbarIndex ? newColor.color:'',borderColor:newColor.color}" :class="[tbIndex == tabbarIndex ? 'on' : '']">{{ grid }}</view>
			</view>
		</view>
		<!-- 考虑非APP端长列表和复杂的DOM使用scroll-view会卡顿，所以漂浮顶部选项卡使用page本身的滑动 -->
		<view class="order-list">
			<view class="list">
				<view class="onorder" v-if="list.length == 0">
					<!-- <image src="../../../static/img/nullorder.png"></image> -->
					<image src="../../../static/img/home/notdata.png" mode=""></image>
					<view class="text">暂 无 订 单 信 息</view>
				</view>
				<view class="row" v-for="(row, index) in list" :key="index">
					<view class="type">
						<!-- <image :src="row.img[0]" mode=""></image> -->
						<text class="name">订单号:{{row.order_no}}</text>
						<text :style="{color:newColor.color}">{{ typeText[row.status + 1] }}</text>
					</view>
					<view class="order-info">
						<view class="checkbox-box" @tap="selected(index)" v-if="tabbarIndex == 1 && type == 1">
							<view class="checkbox" :style="{borderColor:newColor.color}"><view :class="[row.selected ? 'on' : '']" :style="{background:isAllselected ? newColor.color:'' }"></view></view>
						</view>
						<view class="left"><image :src="row.img[0]" @tap="jumpDetail(row)"></image></view>
						<view class="right">
							<view class="name" @tap="jumpDetail(row)">{{ row.title }}</view>
							<view class="spec">规格:{{row.option_title || '暂无规格'}}</view>
							<view class="price-number">
								￥
								<view class="price">{{ row.price }}</view>
								<view class="number">x{{ row.num }}</view>
							</view>
						</view>
					</view>
					<view class="detail">
						<view class="number" :style="{color:newColor.color}">共{{ row.num }}件商品</view>
						<view class="sum" :style="{color:newColor.color}">
							合计:￥
							<view class="price" :style="{color:newColor.color}">{{ row.total_price }}</view>
						</view>
					</view>
					<view class="btns" v-if="row.status == 2 && type == 1">
						<block><view class="default" :style="{color:newColor.color,borderColor:newColor.color}" @tap="remindDeliver(row)">确认到货</view></block>
					</view>
				</view>
			</view>
		</view>
		<!-- 只在备货中的选项显示 -->
		<view class="footer" :style="{ bottom: footerbottom }" v-if="list.length !== 0 && tabbarIndex == 1 && type == 1">
			<view class="checkbox-box" @tap="allSelect">
				<view class="checkbox" :style="{borderColor:newColor.color}"><view :class="[isAllselected ? 'on' : '']" :style="{background:isAllselected ? newColor.color:'' }"></view></view>
				<view class="text">全选</view>
			</view>
			<view class="settlement">
				<!-- <view class="sum" :style="{color:newColor.color}">
					数量:
					<view class="money">{{ goodlength }}</view>
				</view> -->
				<view class="btn" :style="{background:newColor.color}" @tap="toConfirmation">一键确认到货</view>
			</view>
		</view>
		<ourLoading isFullScreen :active="active" text="loading..." />
	</view>
</template>
<script>
export default {
	data() {
		return {
			headerPosition: 'fixed',
			headerTop: '0px',
			active: true,
			typeText: ['已取消','待支付','备货中','备货中','配送中','已完成'],
			orderType: ['全部', '备货中',"配送中",  '交易成功'],
			//订单列表 演示数据
			orderList: [
				
			],
			list: [],
			selectedList:[],
			tabbarIndex: 0,
			isAllselected:false,
			footerbottom:'',
			goodlength: 0,
			typeid:'',
			status: '',
			dataList:[],
			pageIndex: 1,
			type:''
			
		};
	},
	onLoad(option) {
		//option为object类型，会序列化上个页面传递的参数
		// console.log("option: " + JSON.stringify(option));
		let tbIndex = parseInt(this.$Route.query.tbIndex) + 1 || 0;
		console.log(tbIndex);
		this.list = this.orderList[tbIndex];
		this.tabbarIndex = tbIndex;
		//兼容H5下排序栏位置
		// #ifdef H5
		let Timer = setInterval(() => {
			let uniHead = document.getElementsByTagName('uni-page-head');
			if (uniHead.length > 0) {
				this.headerTop = uniHead[0].offsetHeight + 'px';
				clearInterval(Timer); //清除定时器
			}
		}, 1);
		// #endif
		//兼容H5下结算条位置
		// #ifdef H5
		this.footerbottom = document.getElementsByTagName('uni-tabbar')[0].offsetHeight + 'px';
		// #endif
	},
	onPageScroll(e) {
		return;
		//兼容iOS端下拉时顶部漂移
		this.headerPosition = e.scrollTop >= 0 ? 'fixed' : 'absolute';
	},
	onPullDownRefresh() {
		console.log('下拉刷新');
		let that = this;
		that.pageIndex = 1;
		setTimeout(function() {
			that.getOrder();
			uni.stopPullDownRefresh();
		}, 500);
	},
	onReachBottom: function() {
		// pullScroll.success();
		console.log('上拉加载');
		let that = this;
		setTimeout(() => {
			that.pageIndex++;
			that.request('order/getOrderList', {
				type: that.type,
				type_id: that.typeid,
				page: that.pageIndex,
				psize: 10,
				status: that.status,
				group_success:'',
				
			}).then(res => {
				console.log(res);
				if (res.code == 200) {
					let newdata = res.data;
					that.list = that.list.concat(newdata);
				}
				if (res.data.length == 0) {
					uni.showToast({
						title: '已无更多数据',
						icon: 'none'
					});
				}
			});
		}, 1000);
	},
	onShow() {
		this.type = this.$Route.query.status
		this.getMyinfo()
		setTimeout(() => {
			this.active = false;
		}, 600);
	},
	methods: {
		showType(tbIndex) {
			this.tabbarIndex = tbIndex;
			this.pageIndex = 1
			this.list = []
			if(tbIndex == 0){
				this.status = ''
			}else if(tbIndex == 1){
				this.status = "1,2"
			}else if(tbIndex == 2){
				this.status = 3
			}else if(tbIndex == 3){
				this.status = 4
			}
			this.getOrder()
		},
		showLogistics(row) {},
		remindDeliver(row) { //提货点确认到货
			let that = this;
			uni.showModal({
				title: '提示',
				content: '是否确认到货?',
				confirmColor: this.newColor.color,
				success: res => {
					if(res.confirm){
						uni.showLoading({
							title:'提交中...'
						})
						that.request('order/goodsPointReReceiveOrder', {
							order_id: row.id,
							goods_point_id: that.typeid
						}).then(res => {
							console.log(res);
							uni.hideLoading()
							if (res.code == 200) {
								uni.showToast({
									title: '确认到货成功!'
								});
								that.pageIndex = 1
								that.getOrder();
							}
							if (res.code == 400) {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								});
							}
						});
					}
				}
			});
		},
		toConfirmation(){//一键确认到货
			let data = {}
			data.id = this.selectedList.join(',')
			this.remindDeliver(data)
		},
		jumpDetail(row){
			this.$Router.push({path:'/pagesA/pages/other/otherOrderDetails',query:{
				orderid: row.id
			}})
		},
		cancelOrder(row) {
			uni.showModal({
				title: '取消订单',
				content: '确定取消此订单？',
				success: res => {
					if (res.confirm) {
						console.log('用户点击确定');
						this.doCancelOrder(row.ordersn);
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		doCancelOrder(ordersn) {
			let typeNum = this.orderList.length;
			for (let i = 0; i < typeNum; i++) {
				let list = this.orderList[i];
				let orderNum = list.length;
				if (orderNum > 0 && list[0].type == 'unpaid') {
					for (let j = 0; j < orderNum; j++) {
						if (this.orderList[i][j].ordersn == ordersn) {
							this.orderList[i][j].type = 'cancelled';
							break;
						}
					}
				}
			}
		},
		toPayment(row) {
			//本地模拟订单提交UI效果
			uni.showLoading({
				title: '正在获取订单...'
			});
			var paymentOrder = [];
			console.log(row);
			paymentOrder.push(row);
			setTimeout(() => {
				uni.setStorage({
					key: 'paymentOrder',
					data: paymentOrder,
					success: () => {
						uni.hideLoading();
						this.$Router.push({
							path: '/pagesA/pages/order/confirmation',
							query: {
								amount: row.payment
							}
						});
					}
				});
			}, 500);
		},
		selected(index){ //单选
			let status = this.list[index].selected ? false : true;
			this.$set(this.list[index],'selected', status)
			let i = this.selectedList.indexOf(this.list[index].id);
			if(i > -1){
				console.log('存在了')
				this.selectedList.splice(i, 1)
			}else{
				console.log('不存在')
				this.selectedList.push(this.list[index].id)
			}
			// 全选的判断
			this.isAllselected = this.selectedList.length == this.list.length;
			// 数量
			this.goodlength = this.selectedList.length
		},
		allSelect() { //全选
			let len = this.list.length;
			let arr = [];
			for (let i = 0; i < len; i++) {
				this.list[i].selected = this.isAllselected ? false : true;
				arr.push(this.list[i].id);
			}
			this.selectedList = this.isAllselected ? [] : arr;
			this.isAllselected = this.isAllselected || this.list.length == 0 ? false : true;
			// 数量
			this.goodlength = this.selectedList.length
			console.log(this.selectedList)
		},
		getMyinfo(){
			this.request('user/getMyLeaderGoodsPoint',{
				type: this.type
			}).then(res=>{
				console.log(res)
				this.typeid = res.data[0].id
				this.getOrder()
			})
		},
		getOrder(){
			this.request('order/getOrderList',{
				type: this.type,
				type_id: this.typeid,
				page: this.pageIndex,
				psize: 10,
				status: this.status,
				group_success:''
			}).then(res=>{
				console.log(res)
				this.list = res.data
			})
		}
	}
};
</script>

<style lang="scss" scoped>
.topTabBar {
	width: 100%;
	position: fixed;
	top: 0;
	z-index: 10;
	background-color: #ffffff !important;
	height: 80upx;
	display: flex;
	justify-content: space-around;
	.grid {
		width: 20%;
		height: 80upx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #999999;
		font-size: 28upx;
		.text {
			height: 76upx;
			display: flex;
			align-items: center;
			&.on {
				color: #f23a3a;
				border-bottom: solid 4upx #f23a3a;
			}
		}
	}
}
.order-list {
	margin-top: 80upx;
	padding-top: 20upx;
	width: 100%;
	.list {
		width: 94%;
		margin: 0 auto;
		margin-bottom: 120upx;
		transition: all 0.3s;
		
		.onorder {
			width: 100%;
			height: 50vw;
			display: flex;
			justify-content: center;
			align-content: center;
			flex-wrap: wrap;
			margin-top: 80upx;
			image {
				width: 220upx;
				height: 190upx;
			}
			.text {
				width: 100%;
				height: 60upx;
				font-size: 28upx;
				font-family: Droid Sans Fallback;
				font-weight: 400;
				color: rgba(153, 153, 153, 1);
				line-height: 60upx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.row {
			width: calc(100% - 40upx);
			padding: 20upx 20upx;
			border-radius: 10upx;
			background-color: #fff;
			margin-bottom: 20upx;
			box-sizing: content-box;
			.type {
				height: 60upx;
				// display: flex;
				align-items: center;
				text-align: right;
				font-size: 28upx;
				font-family: Droid Sans Fallback;
				font-weight: bold;
				color: rgba(242, 58, 58, 1);
				line-height:60upx;
				margin-bottom: 20upx;
				image{
					width: 50upx;
					height: 50upx;
					float: left;
					border-radius: 50%;
					margin-top: 5upx;
				}
				.name{
					float: left;
					font-size:24upx;
					font-family:Droid Sans Fallback;
					font-weight:400;
					color:rgba(153,153,153,1);
					margin-left: 10upx;
				}
			}
			.order-info {
				width: 100%;
				display: flex;
				.left {
					flex-shrink: 0;
					// width: 25vw;
					// height: 25vw;
					width: 200upx;
					height: 200upx;
					image {
						// width: 25vw;
						// height: 25vw;
						width: 200upx;
						height: 200upx;
						border-radius: 10upx;
					}
				}
				.right {
					width: 100%;
					margin-left: 20upx;
					position: relative;
					.name {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					.spec {
						color: #a7a7a7;
						font-size: 24upx;
					}
					.price-number {
						position: absolute;
						bottom: 0;
						width: 100%;
						display: flex;
						color: rgba(51, 51, 51, 1);
						align-items: flex-end;
						font-size: 28upx;
						.price {
							margin-right: 5upx;
							font-size: 28upx;
							font-family: Droid Sans Fallback;
							font-weight: 400;
							color: rgba(51, 51, 51, 1);
						}
						.number {
							font-size: 22upx;
							font-family: Droid Sans Fallback;
							font-weight: 400;
							color: rgba(153, 153, 153, 1);
						}
					}
				}
			}
			.detail {
				display: flex;
				justify-content: flex-end;
				align-items: flex-end;
				height: 60upx;
				font-size: 24upx;
				color: #f23a3a;
				// padding-bottom: 20upx;
				.sum {
					padding: 0 8upx;
					display: flex;
					align-items: flex-end;
					margin-right: 20upx;
					.price {
						font-size: 30upx;
					}
				}
			}
			.btns {
				height: 80upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				border-top: 1upx solid #F2F2F2;
				margin-top: 20upx;
				view {
					min-width: 120upx;
					height: 50upx;
					padding: 0 20upx;
					border-radius: 38upx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 24upx;
					margin-left: 20upx;
					line-height: 50upx;
				}
				.default {
					border: solid 1upx #f23a3a;
					color: #f23a3a;
				}
			}
		}
	}
}
.checkbox-box {
	display: flex;
	align-items: center;
	margin-right: 20upx;
	.checkbox {
		width: 35upx;
		height: 35upx;
		border-radius: 50%;
		border: solid 1upx #ff0000;
		position: relative;
		
		.on {
			width: 25upx;
			height: 25upx;
			border-radius: 50%;
			background-color: #ff0000;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
		}
	}
	.text {
		margin-left: 10upx;
		font-size: 28upx;
		font-family: Adobe Heiti Std;
		font-weight: normal;
		color: rgba(51, 51, 51, 1);
	}
}
.footer {
	width: 100%;
	padding: 0 4%;
	background-color: #fbfbfb;
	height: 100upx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 28upx;
	position: fixed;
	bottom: 0upx;
	z-index: 5;
	.delBtn {
		border: solid 1upx #ff0000;
		color: #ff0000;
		padding: 0 20upx;
		height: 40upx;
		border-radius: 30upx;
		display: flex;
		font-size: 24upx;
		justify-content: center;
		align-items: center;
	}
	.settlement {
		width: 60%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		.sum {
			width: 50%;
			font-size: 24upx;
			margin-right: 10upx;
			display: flex;
			justify-content: flex-end;
			font-family: Droid Sans Fallback;
			font-weight: 400;
			color: rgba(255, 0, 0, 1);
			.money {
				font-size: 24upx;
				font-family: Droid Sans Fallback;
				font-weight: 400;
				color: rgba(255, 0, 0, 1);
			}
		}
		.btn {
			width: 280upx;
			height: 76upx;
			line-height: 76upx;
			text-align: center;
			background: rgba(255, 0, 0, 1);
			border-radius: 38px;
			color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
}
</style>
<style lang="scss">
	page{
		background-color: #f3f3f3;
	}
</style>
